/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Loans;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Dine
 */
public class LoansDAO extends DAO {

    public LoansDAO() {
        super();
    }
    //BASIC DAO METHODS

    @SuppressWarnings("unchecked")
    public List<Loans> getAll() throws Exception {
        List<Loans> a = (List<Loans>) getSession().createQuery("from Loans a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Loans get(int id) throws Exception {
        Loans a = (Loans) getSession().createQuery("from Loans a where a.loanid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Loans a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Loans a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Loans> findByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        List<Loans> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public List<Loans> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Loans> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }

    //OTHER METHODS
    @SuppressWarnings("unchecked")
    public Loans getLoansByBCode(int bcode, int acode) throws Exception {
        Loans c = (Loans) getSession().createQuery("from Loans c where c.borrowers.borrowerid=:bcode and c.status=:status and c.areas.areaid=:acode").setParameter("bcode", bcode).setString("status", "active").setParameter("acode", acode).setMaxResults(1).setCacheable(true).uniqueResult();
        flush();
        return c;
    }

    @SuppressWarnings("unchecked")
    public Loans getLoansByBorrowerUnique(int bcode) throws Exception {
        Loans c = (Loans) getSession().createQuery("from Loans c where c.borrowers.borrowerid=:bcode and c.status=:status").setParameter("bcode", bcode).setString("status", "active").setMaxResults(1).setCacheable(true).uniqueResult();
        //System.out.println("getloans:" + c);
        flush();
        return c;
    }

    @SuppressWarnings("unchecked")
    public List<Loans> getLoansByBorrower(int bcode) throws Exception {
        List<Loans> c = getSession().createQuery("from Loans c where c.borrowers.borrowerid=:bcode and c.status=:status").setParameter("bcode", bcode).setString("status", "active").list();
        flush();
        return c;
    }

    @SuppressWarnings("unchecked")
    public Loans getLatestLoansByBorrower(int bid) throws Exception {
        Loans c = (Loans) getSession().createQuery("from Loans c where c.borrowers.borrowerid=:bcode order by c.loanid desc").setParameter("bcode", bid).setMaxResults(1).setCacheable(true).uniqueResult();
        flush();
        return c;
    }

    @SuppressWarnings("unchecked")
    public void setBalAndOverdue(int loanid, double balance, double overdue) throws Exception {
        try {
            begin();
            getSession().createQuery("update Loans set balance =:balance where loanid=:loanid").setParameter("balance", balance).setParameter("loanid", loanid).executeUpdate();
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Loans> getByStatus(String status) throws Exception {
        List<Loans> loans = getSession().createQuery("from Loans where status = :status").setParameter("status", status).setCacheable(true).list();
        flush();
        return loans;
    }

    @SuppressWarnings("unchecked")
    public List<Loans> getByStatus(String status, int branchid) throws Exception {
        List<Loans> loans = getSession().createQuery("from Loans l where l.status = :status and l.branches.branchid = :branchid").setParameter("status", status).setParameter("branchid", branchid).setCacheable(true).list();
        flush();
        return loans;
    }
}
